#include "downloadermainwindow.h"
#include "centerform.h"
#include "configurationform.h"

DownloaderMainWindow::DownloaderMainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    centerForm = new CenterForm();
    this->setCentralWidget(centerForm);
    this->createMenus();
    connect(this, SIGNAL(tableLayoutChanged(int)), this->centerForm, SLOT(deleteRaw(int)));
    adjustSize();
}

DownloaderMainWindow::~DownloaderMainWindow()
{
    delete menuBar;
    delete centerForm;
}

void DownloaderMainWindow::createMenus()
{
    menuBar = new QMenuBar(this);

    QAction* startAct = new QAction("+", this);
    connect(startAct, SIGNAL(triggered()), this, SLOT(start()));

    QAction* delAct = new QAction("-", this);
    connect(delAct, SIGNAL(triggered()), this, SLOT(deleteDownload()));

    menuBar->addAction(startAct);
    menuBar->addAction(delAct);

    this->setMenuBar(menuBar);
}

void DownloaderMainWindow::start()
{
    ConfigurationForm *configuration = new ConfigurationForm(0);
    connect(configuration, SIGNAL(dbUpdate()), this->centerForm, SLOT(dataUpdate()));
    configuration->show();
}

void DownloaderMainWindow::deleteDownload()
{
    emit tableLayoutChanged(centerForm->table->currentIndex().row());
}

void DownloaderMainWindow::showEvent(QShowEvent *)
{
    centerForm->show();
}

void DownloaderMainWindow::closeEvent(QCloseEvent *event)
{
    DatabaseManager::getDBManager()->close();
    event->accept();
}
